package com.qkl.xiatang.app.web.controller;

import com.qkl.xiatang.app.dto.user.UserApplyWebDTO;
import com.qkl.xiatang.app.dto.user.UserApproveWebDTO;
import com.qkl.xiatang.app.dto.user.UserQueryInDTO;
import com.qkl.xiatang.app.service.impl.AppUserManagerService;
import com.qkl.xiatang.app.utils.AppUtils;
import com.qkl.xiatang.app.utils.ErrorCodeUtils;
import com.qkl.xiatang.app.utils.Result;
import com.qkl.xiatang.app.utils.appenum.AppUserDutiesEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("{app.baseURL}")
@Slf4j
public class AppUserManagerController {

    @Autowired
    private AppUserManagerService appUserManagerService;

    /**
     * 职位列表
     * @return
     */
    @GetMapping("/userDutiesList")
    public Result userDutiesList(@RequestParam Map<String,String> params){
        String userId = StringUtils.trimToEmpty(params.get("userId"));
        return ControllerHelper.toQueryResult(appUserManagerService.userDutiesList(userId));
    }

    /**
     * 查询申请用户
     * @param params
     * @return
     */
    @GetMapping("/queryApplyUser")
    public Result queryApplyUser(@RequestParam Map<String,String> params){
        return ControllerHelper.toQueryResult(appUserManagerService.queryApplyUser(toUserQueryInDTO(params)));
    }

    /**
     * 查询正常用户
     * @param params
     * @return
     */
    @GetMapping("/queryNormalUser")
    public Result queryNormalUser(@RequestParam Map<String,String> params){
        return ControllerHelper.toQueryResult(appUserManagerService.queryNormalUser(toUserQueryInDTO(params)));
    }

    /**
     * 用户申请加入公司
     * @param params
     * @return
     */
    @GetMapping("/userApplyCompany")
    public Result userApplyCompany(@RequestParam Map<String,String> params){
        UserApplyWebDTO webDTO = new UserApplyWebDTO();
        webDTO.setCompanyId(params.get("companyId"));
        return ControllerHelper.toOperateResult(appUserManagerService.userApplyCompany(webDTO));
    }

    /**
     * 用户取消加入公司申请
     * @param params
     * @return
     */
    @GetMapping("/userApplyCompanyUndo")
    public Result userApplyCompanyUndo(@RequestParam Map<String,String> params){
        String companyId = StringUtils.trimToEmpty(params.get("companyId"));
        if(StringUtils.isEmpty(companyId)){
            return Result.error(ErrorCodeUtils.PARAM_EMPTY, ErrorCodeUtils.PARAM_EMPTY_MSG);
        }
        UserApplyWebDTO webDTO = new UserApplyWebDTO();
        webDTO.setCompanyId(companyId);
        return ControllerHelper.toOperateResult(appUserManagerService.userApplyCompanyUndo(webDTO));
    }

    /**
     * 审批用户申请
     * @param webDTO
     * @return
     */
    @PostMapping("/userApplyApprove")
    public Result userApplyApprove(@RequestBody UserApproveWebDTO webDTO) {
        return ControllerHelper.toOperateResult(appUserManagerService.userApplyApprove(webDTO));
    }

    /**
     * 设置用户身份
     * @param webDTO
     * @return
     */
    @PostMapping("/grantUserIdentity")
    public Result grantUserIdentity(@RequestBody UserApproveWebDTO webDTO) {
        return ControllerHelper.toOperateResult(appUserManagerService.grantUserIdentity(webDTO));
    }

    /**
     * 将用户踢出公司
     * @param params
     * @return
     */
    @GetMapping("/removeUserCompany")
    public Result removeUserCompany(@RequestParam Map<String,String> params){
        String userId = StringUtils.trimToEmpty(params.get("userId"));
        if(StringUtils.isEmpty(userId)){
            return Result.error(ErrorCodeUtils.PARAM_EMPTY, ErrorCodeUtils.PARAM_EMPTY_MSG);
        }
        UserApproveWebDTO webDTO = new UserApproveWebDTO();
        webDTO.setUserId(userId);
        return ControllerHelper.toOperateResult(appUserManagerService.removeUserCompany(webDTO));
    }

    private static UserQueryInDTO toUserQueryInDTO(Map<String,String> params){
        String pageIndex = StringUtils.trimToEmpty(params.get("pageIndex"));
        String pageSize = StringUtils.trimToEmpty(params.get("pageSize"));
        String keywords = StringUtils.trimToEmpty(params.get("keywords"));
        UserQueryInDTO webDTO = new UserQueryInDTO();
        webDTO.setPageIndex(AppUtils.parseInt(pageIndex));
        webDTO.setPageSize(AppUtils.parseInt(pageSize));
        webDTO.setKeywords(keywords);
        return webDTO;
    }

}
